package com.palace.saw.module.app.service.banner;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.palace.saw.module.app.controller.admin.banner.vo.*;
import com.palace.saw.module.app.dal.dataobject.banner.BannerDO;
import com.palace.saw.framework.common.pojo.PageResult;
import com.palace.saw.framework.common.pojo.PageParam;
import com.palace.saw.framework.common.util.object.BeanUtils;

import com.palace.saw.module.app.dal.mysql.banner.BannerMapper;

import static com.palace.saw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.palace.saw.module.app.enums.ErrorCodeConstants.*;

/**
 * Banner 广告位 Service 实现类
 *
 * @author saw
 */
@Service
@Validated
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper;

    @Override
    public Long createBanner(BannerSaveReqVO createReqVO) {
        // 插入
        BannerDO banner = BeanUtils.toBean(createReqVO, BannerDO.class);
        bannerMapper.insert(banner);
        // 返回
        return banner.getId();
    }

    @Override
    public void updateBanner(BannerSaveReqVO updateReqVO) {
        // 校验存在
        validateBannerExists(updateReqVO.getId());
        // 更新
        BannerDO updateObj = BeanUtils.toBean(updateReqVO, BannerDO.class);
        bannerMapper.updateById(updateObj);
    }

    @Override
    public void deleteBanner(Long id) {
        // 校验存在
        validateBannerExists(id);
        // 删除
        bannerMapper.deleteById(id);
    }

    private void validateBannerExists(Long id) {
        if (bannerMapper.selectById(id) == null) {
            throw exception(BANNER_NOT_EXISTS);
        }
    }

    @Override
    public BannerDO getBanner(Long id) {
        return bannerMapper.selectById(id);
    }

    @Override
    public PageResult<BannerDO> getBannerPage(BannerPageReqVO pageReqVO) {
        return bannerMapper.selectPage(pageReqVO);
    }

}